﻿Option Explicit On


Module mdlLOGGING

    Public Sub fLOG(ByVal LLogDesc As String, ByVal LProfileNumber As Integer)

        Dim cnDestination As New SQLite.SQLiteConnection ' ADODB.Connection()
        Dim daDestination As New SQLite.SQLiteDataAdapter 'Dim rsDestination As New ADODB.Recordset()
        Dim sqlCommand As New SQLite.SQLiteCommand
        Dim sSQL As String

        'Dim sErrorMsg As String

        'Exit Sub

        'Dim LUsername As String = frmMDIBOK.cUser.UserID
        Dim LUsername As String = ""

        Try
            Dim sDesc As String

            ' LOGGING
            'SQL Verbinding

            cnDestination.ConnectionString = fGetConnectionString()
            cnDestination.Open()

            '================================
            '==== Indien in debug mode : ====
            '================================
            If System.Diagnostics.Debugger.IsAttached = False Then
                '*// Applicatie gewoon opgestart:
                'LLogDesc = LLogDesc
            Else '*// Applicatie vanuit VB.NET opgestart (debug)
                LLogDesc = "IDE - " & LLogDesc
            End If
            '================================

            If LProfileNumber = 99 Then
                LLogDesc = Environment.MachineName & " - " & LLogDesc
            End If


            If LLogDesc.Length > 1996 Then
                sDesc = LLogDesc.Substring(0, 1996).Replace("'", "''") & " ..." '*// total 2000 character
            Else
                sDesc = LLogDesc.Replace("'", "''")
            End If

            sSQL = "INSERT INTO LOG" _
                        & " (LogDT, LogUserID, LogDesc, LogProfileID)" _
                    & " VALUES" _
                        & " (" _
                            & "'" & Format(System.DateTime.Now, "yyyy-MM-dd HH:m:ss:fff") & "'," _
                            & "'" & LUsername & "'," _
                            & "'" & sDesc & "'," _
                            & "'" & LProfileNumber & "'" _
                        & " )"
            sqlCommand.Connection = cnDestination
            sqlCommand.CommandText = sSQL
            'sqlCommand.ExecuteNonQuery()
            'cnDestination.Execute(sSQL)

            cnDestination.Close()

            '/////////////////////////
            '*// Bij ERRORS (ProfileNumber 99) geven we ook een melding aan de eindgebruiker. 
            '*// Andere logs worden gewoon opgeslagen.
            If LProfileNumber = 99 Then

                '*// Om een hele reeks foutboodschappen achter elkaar te vermijden: 
                If Not isFormOpen(frmError.Name) Then
                    'Dim objError As New clsErrorScreen
                    'objError.ShowErrorScreen(sErrorMsg)
                    'objError = Nothing
                    frmError.SetMessage(LLogDesc)
                    frmError.ShowDialog()
                End If
            End If


        Catch ex As Exception
            MsgBox("Error while logging")

        Finally
            cnDestination = Nothing

        End Try
    End Sub
    Public Function isFormOpen(ByVal formName As String) As Boolean
        '*////////////////////////////////////////////////////////////////////////////////////////////
        '*/ bron: http://www.satalketo.co.uk/vbnet-articles/check-whether-an-instance-of-a-form-is-open
        '*////////////////////////////////////////////////////////////////////////////////////////////


        Dim frm As Form
        Dim i As Integer = 0
        Dim found As Boolean = False
        While i < Application.OpenForms.Count
            frm = Application.OpenForms.Item(i)
            If frm.Name.ToLower = formName.ToLower Then
                found = True
                Exit While
            End If
            i += 1
        End While
        Return found
    End Function
End Module
